import { EnumUserType } from '../../utils/constant.js'
const app = getApp(),url = app.globalData.url;
Page({
  data: {
    url : app.globalData.url,
    isQiYe: true,
    isRCLoading: false,
    // {id:3,img:'/img/5.png',title:'假本地信息',url:'/HomePage/information/information?id=3'},
    // {id:4,img:'/img/5.png',title:'假职场资讯',url:'/HomePage/workplace/workplace?id=4'},
    nav:[
      {id:1,img:'/img/5.png',title:'企业库',url:"/HomePage/enterprise/enterprise?id=1"},
      {id:2,img:'/img/5-1.png',title:'招聘会',url:'/HomePage/jobfair/jobfair?id=2'},
      {id:5,img:'/img/5-2.png',title:'本地信息',url:'/pages/news/news?id=1'},
      {id:4,img:'/img/5-3.png',title:'职场资讯',url:'/pages/news/news?id=2'},
    ],
    //banner 此处url 可以不设计
    shopbanner:[
      // {picurl:'/img/6.png',url:'/HomePage/search/search?id=15'},{picurl:'/img/6.png',url:'/HomePage/search/search?id=25'}
    ],
    all_new:[
      {id:1,name:'全部',val:'mr'},{id:2,name:'热门',val:'zx',}
    ],
    search:{
      pageNum:1,
      pageSize:10,
      pxgz:'mr',
    },
    where:{
      pageNum:1,
      pageSize:10,
      pxgz:'mr',
      // user_uid:'',
    },
    Nid:1,
    // 列表
    listdata:[],
    all_where:{
      pageNum:1,
      pageSize:10,
      pxgz: 'mr'
      // user_uid:'',
    },

    rcList:[],
    hotJobList:[],
    isHotLoading: false,

    jobList:[],
    isJobLoading: false
  },
  /**
   * ,
    "navigationStyle":"custom"  
  */
  onLoad (options) {
    this.userInfo = wx.getStorageSync('userInfo')
    let userType = wx.getStorageSync('userType')
    let isQiYe = userType === EnumUserType.QiYe
    this.setData({ isQiYe })
    let that = this
    // wx.setNavigationBarTitle({title: '东港人社服务大厅'})
    app.W_get(url + "banner/getBannerPic", {
      pic_type:'m_banner'// m_head_pic
		} , res => {
			let ret = res.data	
			if(ret.code == 200){
        that.setData({
          shopbanner:ret.data,
          img:ret.data
        })
      }else{
        wx.showToast({
          title: ret.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
    
    if(isQiYe){
      this.data.nav[0] = {id:1,img:'/img/nav_rc.png',title:'招人才',url:"/pages/rencai/rencai"}
      this.setData({
        nav: this.data.nav
      })
      this.getRecommendRC()
      return
    }
    this.getHotJobs()
    this.getPositionList()

    // this.all_list()

  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
        let tabBar = this.getTabBar()
        tabBar.setData({ selected: 0 })
      if(this.data.isQiYe){
        let navList = tabBar.data.list
        navList[1] = {
          pagePath: "/pages/rencai/rencai",
          iconPath: "/img/tab/2-1.png",
          selectedIconPath: "/img/tab/2.png",
          text: "人才"
        }
        tabBar.setData({
          list: navList
        })
      }
    }
    this.setData({Nid:1})//在其他页面回来 重新赋值
  },
  getRecommendRC(){
    let params = {
      id: this.userInfo.id,
      ...this.data.search
    }
    this.setData({isRCLoading: true})
    app.W_get(url + "recommend/getRecommendPersonList", params, res => {
      this.setData({isRCLoading: false})
      let resData = res.data
      if(resData.code != 200){
        return
      }
      
      this.setData({
        rcList: this.data.rcList.concat(resData.data.content),
      })
      if(params.pageNum > 1 && params.pageNum >= resData.data.totalPages){
        wx.showToast({
          title: '到底了~~',
          icon:'none'
        })
      }
    }, err => {
      this.setData({isRCLoading: false})
    })
  },
  getHotJobs(){
    this.setData({isHotLoading: true})
    app.W_get(url + "position/searchRm",{}, res => {
      this.setData({isHotLoading: false})
      let resData = res.data
      this.setData({
        hotJobList: resData,
      })
    }, err => {
      this.setData({isHotLoading: false})
    })
  },
  toJianLi(e){
    let { item } = e.currentTarget.dataset
    wx.setStorageSync('jl_header', item.header)
    wx.navigateTo({
      url: '/HomePage/linejianli/linejianli?type=view&uid='+ item.userUid,
    })
  },
  getPositionList(){
    let userInfo = wx.getStorageSync('user_massage')
    let api = ''
    let params = Object.assign({}, this.data.search)
    if(userInfo && userInfo.userUid){
      params.user_uid = userInfo.userUid
      api = url + "position/getPositionXcxByPageInfoNew"
    } else {
      api = url + "position/getPositionByPageInfoNew"
    }
    let _this = this
    wx.request({
      url: api,
      method: "GET",
      data:params,
      success(res){
        let resData = res.data
        if(resData.code != 200){
          return
        }
        let list = []
        // if(!userInfo){
          list = resData.data && (resData.data.content || [])
        // } else{
        //   list = resData.data || []
        // }
        _this.setData({
          listdata: _this.data.listdata.concat(list),
        })
        if(params.pageNum > 1 && !list.length){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
        }
      }
    })
  },
  // 搜索
  search(){
    wx.navigateTo({
      url: '/HomePage/search/search',
    })
  },
  // 点击导航
  jump(e){
    var that = this
    that.data.where.pageNum = 1
    that.data.all_where.pageNum = 1
    let url = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.id
    if(url.startsWith("/pages/")){
      wx.setStorageSync('si_or_wu', id)
      wx.switchTab({ url })
      return
    }
     
    let uid = wx.getStorageSync(this.data.isQiYe ? 'userInfo' : 'user_massage')
    if(uid){
      wx.navigateTo({ url })
      return
    }
    wx.showModal({
      title: '提示',
      content: '你还未登录，是否前往登录？',
      success (res) {
        if (res.confirm) {
          wx.switchTab({ url: '/pages/my/my' })
          // wx.navigateTo({ url: '/HomePage/login/login' })
        }
      }
    })
  },
  //点击类别 全部  最新
  butType(e){
    let that = this
    let tid = e.currentTarget.dataset.id
    let val = e.currentTarget.dataset.val
    that.setData({Nid:tid,listdata:[]})
    this.data.search.pageNum = 1
    this.data.search.pxgz = val
    this.getPositionList()
    // if(tid == 2){
    //   that.data.where.pageNum = 1
    //   that.data.where.pxgz = val
    //   that.list()
    // }else{
    //   that.data.all_where.pageNum = 1
    //   that.data.all_where.pxgz = val
    //   that.all_list()
    // }
  },
  list(){
    var that = this
    // var uid = wx.getStorageSync('user_massage')
    // that.data.where.user_uid = uid.userUid || ''
    let where = that.data.where
    console.log(where)
    wx.request({
      url:url+'position/getPositionXcxByPageInfoNew',//getPositionByPageInfoNew
      method: "GET",
      data:where,
      success:function(res){
        console.log("职位",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({
            listdata:ret.data//.content
          })
        }else{
          that.setData({listdata:[]})
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  detail(e){
    var that = this
    let did = e.currentTarget.dataset.id
    let isColl = e.currentTarget.dataset.iscoll
    that.data.where.pageNum = 1
    that.data.all_where.pageNum = 1
    wx.navigateTo({
      url: '/HomePage/workdetail/workdetail?id='+did,//+'&isColl='+isColl
    })
  },
  
  all_list(){
    var that = this
    // var uid = wx.getStorageSync('user_massage')
    // that.data.all_where.user_uid = uid.userUid || ''
    let where = that.data.all_where
    wx.request({
      url:url+'position/getPositionByPageInfoNew',//getAllPositionByPage
      method: "GET",
      data:where,
      success:function(res){
        console.log("全部职位",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({listdata:ret.data.content})//content
        }else{
          that.setData({listdata:[]})
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  /**   * 页面上拉触底事件的处理函数   */
  onReachBottom() {
    this.data.search.pageNum++;
    if(this.data.isQiYe){
      this.setData({
        search: this.data.search
      })
      this.getRecommendRC()
      return
    }
    this.getPositionList()

    
    // var Nid = this.data.Nid
    // if(Nid == 2){//最新
    //   this.listpage()
    // }else{//全部
    //   this.allpage()
    // }
  },
  listpage(){
    var that = this
    that.data.where.pageNum ++
    let where = that.data.where
    let listdata = that.data.listdata//数组合并用返回值
    // console.log('上拉分页加载',where)
    wx.request({
      url:url+'position/getPositionXcxByPageInfoNew',
      method: "GET",
      data:where,
      success:function(res){
        console.log("最新职位",res);
        var ret = res.data
        if(ret.code == 200){
          if(ret.data != ''){//.content
            listdata = listdata.concat(ret.data)//.content
            that.setData({listdata})
          }else{
            wx.showToast({
              title: '到底了~~',
              icon:'none'
            })
          }
        }
      }
    })
  },
  allpage(){
    var that = this
    that.data.all_where.pageNum ++
    let where = that.data.all_where
    let listdata = that.data.listdata//数组合并用返回值
    wx.request({
      url:url+'position/getPositionByPageInfoNew',
      method: "GET",
      data:where,
      success:function(res){
        console.log("全部职位",res);
        var ret = res.data
        if(ret.code == 200){
          if(ret.data.content != ''){//.content
            listdata=listdata.concat(ret.data.content)//.content
            that.setData({listdata})
          }else{
            wx.showToast({
              title: '到底了~~',
              icon:'none'
            })
          }
          // listdata=listdata.concat(ret.data.content)
          // that.setData({listdata})
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
    /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this
    that.data.where.pageNum = 1
    that.data.all_where.pageNum = 1
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    that.data.where.pageNum = 1
    that.data.all_where.pageNum = 1
  },
})